import React, { useEffect, useState, useRef } from "react";
import { Modal, message, Space } from "antd";
import { SearchCheckboxTable } from "@/views/components/SearchTable/index";
import { useTableConfig } from "@/views/components/SearchTable/hooks/useTableConfig";
import AddOutboundOrderModal from "./component/AddOutboundOrderModal";
import SelectActions from "@/views/components/SelectActions";
import OutboundDetailModal from "./component/OutboundDetailModal";
import ForceCompletionModal from "./component/ForceCompletionModal";
import AssignedReturnClerkModal from "./component/AssignedReturnClerkModal";
import useColumns from "./cloumns";
import { getCurrentUser } from "@/utils/authority";
import { getProductOutboundOrderList, getWarehouseList } from "./service";
const GoodsOutboundOrder = () => {
	const [visible, setVisible] = useState("");
	const [actionType, setActionType] = useState("");
	const [record, setRecord] = useState("");
	const [selectedWarehouse, setSelectedWarehouse] = useState(null);
	const [filteredArray, setFilteredArray] = useState([]);
	const tableRequest = getProductOutboundOrderList;
	const warehouseList = useRef();
	const { defaultColumns } = useColumns({
		handleDetailVisible,
		selectedWarehouse,
		handleWarehouseChange,
		warehouseList
	});
	const [columns, setColumns] = useState(defaultColumns);
	const beforeQuerySearch = ({ API_DATA }) => {
		getWarehouseColumns();
		if (!API_DATA.timeType) {
			API_DATA.timeType = 0;
		}
		if (API_DATA.warehouseId && API_DATA.warehouseId.length > 0) {
			API_DATA.warehouseId = API_DATA.warehouseId.map(item => item.value).join(",");
		}

		return API_DATA;
	};
	const tableKey = "GoodsOutboundOrder23131";
	const { selectedKeys, selectedRows, fetchList, dataSource, form, query, searchSimpleTableProps } = useTableConfig({
		tableKey,
		rowKey: "id",
		columns,
		tableRequest,
		autoFetch: true,
		hasPagination: true,
		beforeQuerySearch,
		hasQuickSearch: true,
		resetSourceData: false,
		hasColsSetting: true,
		hasToggleSearchForm: true,
		columnResize: true
	});
	const commonHandleClick = (status, record, type) => {
		if (status == "ForceCompletionModal") {
			// 强制完成
			const filteredArray = selectedRows.filter(item => Number(item.status) == 2);
			if (filteredArray.length > 0) {
				setVisible(status);
				setFilteredArray(filteredArray);
			} else {
				return message.info("没有可操作的数据，请重新勾选");
			}
		} else if (status == "PasseExamine") {
			// 审核出库单
			const filteredArray = selectedRows.filter(item => Number(item.status) == 0);
			if (filteredArray.length > 0) {
				setVisible(status);
				setFilteredArray(filteredArray);
			} else {
				return message.info("没有可操作的数据，请重新勾选");
			}
		} else if (status == "AssignedReturnClerkModal" || status == "Cancellation") {
			// 指派退货员和作废
			const filteredArray = selectedRows.filter(item => Number(item.status) == 0 || Number(item.status) == 1);
			if (filteredArray.length > 0) {
				setVisible(status);
				setFilteredArray(filteredArray);
			} else {
				return message.info("没有可操作的数据，请重新勾选");
			}
		} else {
			if (selectedRows.length || status == "AddOutboundOrderModal") {
				setActionType(type);
				setVisible(status);
			} else {
				Modal.info({ centered: true, content: "请选择操作数据" });
			}
		}
	};
	async function getWarehouseColumns() {
		const res = await getWarehouseList({ uuid: getCurrentUser().uuid });
		const { success, data } = res;
		if (success) {
			const list = [
				{ label: "发货仓", options: [], kind: 1 },
				{ label: "次品仓", options: [], kind: 2 },
				{ label: "销退仓", options: [], kind: 3 }
			];
			(data || []).forEach(item => {
				const ind = list.findIndex(e => e.kind === item.kind);
				list[ind].options.push({
					...item,
					value: item.id,
					label: item.warehouseName
				});
			});
			warehouseList.current = list;
		}
		const filteredArray = columns.filter((item, index) => {
			return columns;
		});
		setColumns(filteredArray);
	}
	async function handleDetailVisible(record, type) {
		setRecord(record);
		setActionType(type);
		if (type == "update") {
			setVisible("AddOutboundOrderModal");
		} else {
			setVisible("OutboundDetailModal");
		}
	}
	async function handleWarehouseChange(value) {
		setSelectedWarehouse(value);
	}

	const CheckGoodsOutboundProps = {
		visible,
		query,
		fetchList,
		record,
		actionType,
		filteredArray,
		warehouseList,
		checkedRows: selectedRows,
		dataSource,
		checkedIds: selectedKeys,
		handleOk() {},
		handleCancel() {
			setVisible("");
		}
	};
	const LeftBtns = (
		<Space>
			<SelectActions name="新建退货出库单" onClick={() => commonHandleClick("AddOutboundOrderModal", "", "add")} />
			<SelectActions name="审核出库单" onClick={() => commonHandleClick("PasseExamine")} />
			<SelectActions name="强制完成" onClick={() => commonHandleClick("ForceCompletionModal")} />
			<SelectActions name="出库单作废" onClick={() => commonHandleClick("Cancellation")} />
			<SelectActions name="指派退货员" onClick={() => commonHandleClick("AssignedReturnClerkModal")} />
		</Space>
	);

	return (
		<>
			<SearchCheckboxTable key={tableKey} leftBtns={LeftBtns} mounted={false} {...searchSimpleTableProps} />
			{visible == "AddOutboundOrderModal" && <AddOutboundOrderModal {...CheckGoodsOutboundProps} />}
			{(visible == "ForceCompletionModal" || visible == "Cancellation" || visible == "PasseExamine") && (
				<ForceCompletionModal {...CheckGoodsOutboundProps} />
			)}
			{visible == "OutboundDetailModal" && <OutboundDetailModal {...CheckGoodsOutboundProps} />}
			{visible == "AssignedReturnClerkModal" && <AssignedReturnClerkModal {...CheckGoodsOutboundProps} />}
		</>
	);
};

export default React.memo(GoodsOutboundOrder, () => {
	return true;
});
